package com.hava.contentsale.web.controller;

import com.hava.contentsale.meta.ProductVO;
import com.hava.contentsale.meta.UserVO;
import com.hava.contentsale.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;

/**
 * Created by yanfa on 2016/11/20.
 * R7 内容的编辑

 R7.1 卖家在查看界面可以点击编辑按钮进入编辑界面
 R7.2 可以修改内容的全部细节：标题、摘要、图片、正文和价格，点击提交后回到查看界面
 */
@Controller
public class EditController {

    @Autowired
    ProductService productServiceImpl;

    //用于显示编辑界面的接口，通过url获取商品的id，查询商品内容并展示到界面让用户修改
    @RequestMapping(value = "/edit")
    public ModelAndView edit_view(HttpSession httpSession, @RequestParam("id") long id)
    {
        System.out.println("Class EditController Method edit_view");
        //session
        System.out.println("[session.id]:" + httpSession.getId());
        UserVO userVO = (UserVO) httpSession.getAttribute("user");

        //当用户没有登录时，跳转回登录界面
        if(userVO == null)
            return new ModelAndView("redirect:login");
            //如果用户类型为卖家，则跳转到发布界面
        else if(userVO.getUsertype() == 1)
        {
            ModelAndView modelAndView = new ModelAndView("edit");
            //当用户不为空时，返回用户信息
            if (userVO != null)
                modelAndView.addObject("user", userVO);

            ProductVO productVO = this.productServiceImpl.findOne(id);
            modelAndView.addObject("product",productVO);
            return modelAndView;
        }
        //由于主访问界面没有错误提示处理，则其他用户类型，直接跳转登录界面
        else
            return new ModelAndView("redirect:login");
    }

    //提交编辑修改的内容接口
    @RequestMapping(value = "/editSubmit",method = RequestMethod.POST)
    public ModelAndView editSubmit(
            @RequestParam("id") long id,
            @RequestParam("title") String title,
            @RequestParam("image") String image,
            @RequestParam("detail") String detail,
            @RequestParam("summary") String summary,
            @RequestParam("price") String price,
            HttpSession httpSession)
    {
        System.out.println("Class EditController Method editSubmit");
        //session
        System.out.println("[session.id]:" + httpSession.getId());
        UserVO userVO = (UserVO) httpSession.getAttribute("user");

        //当用户没有登录时，跳转回登录界面
        if(userVO == null)
            return new ModelAndView("redirect:login");
            //如果用户类型为卖家，则继续处理
        else if(userVO.getUsertype() == 1)
        {

            ModelAndView modelAndView = new ModelAndView("editSubmit");
            //当用户不为空时，返回用户信息
            if (userVO != null)
                modelAndView.addObject("user", userVO);

            ProductVO productVO = new ProductVO();
            productVO.setId(id);
            productVO.setTitle(title);
            productVO.setImage(image);
            productVO.setSummary(summary);
            productVO.setPrice(new BigDecimal(price));
            productVO.setDetail(detail);
            productVO.printProperties();

            try {
                this.productServiceImpl.update(productVO);
                productVO.printProperties();

                modelAndView.addObject("product", productVO);
            }
            catch (Exception e)
            {
                System.out.println("update product exception");
                System.out.println("[Exception]:" + e.toString());
            }

            return modelAndView;
        }
        //由于主访问界面没有错误提示处理，则其他用户类型，直接跳转登录界面
        else
            return new ModelAndView("redirect:login");
    }
}
